<!-- Content Header (Page header) -->
<section class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1>Task file table</h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="#">Home</a></li>
                    <li class="breadcrumb-item active">Task file table</li>
                </ol>
            </div>
        </div>
    </div>
</section>
<section class="content">
    <div class="card">
        <div class="card-header">
            <h3 class="card-title">Task file table</h3>
        </div>
        <div class="card-body p-0">
            <table class="table table-striped projects">
                <thead>
                    <tr>
                        <th style="width: 1%">
                            #
                        </th>
                        <th style="width: 8%">
                            任务ID
                        </th>
                        <th style="width: 13%">
                            任务主题
                        </th>
                        <th style="width: 14%">
                            提交者账号
                        </th>
                        <th style="width: 14%">
                            提交者姓名
                        </th>
                        <th style="width: 13%">
                            文件大小
                        </th>
                        <th>
                            提交时间
                        </th>
                        <th style="width: 20%">
                        </th>
                    </tr>
                </thead>
                <tbody id="filetable_node_container">
                </tbody>
            </table>
        </div>
        <!-- 分页页脚 -->
        <div class="pages"></div>
    </div>
</section>

<script>
    function loadFileTableNode(taskRecordJson) {
        let fileNode =
            ' <tr id=taskRecord_${TASKRECORDID}>' +
            '     <td style="width: 1%">' +
            '            #' +
            '          </td>' +
            '          <td style="width: 8%">' +
            '                <a>' +
            '                       ${TASKID}' +
            '                 </a>' +
            '               <small>' +
            '                ${TASKSTARTDATE}' +
            '             </small>' +
            '      </td>' +
            '         <td style="width: 13%">' +
            '             <small>' +
            '                ${TASKTITLE}' +
            '             </small>' +
            '         </td>' +
            '         <td style="width: 14%">' +
            '              <small>${USERNUMBER}</small>' +
            '        </td>' +
            '        <td style="width: 14%">' +
            '             ${USERNAME}' +
            '         </td>' +
            '       <td style="width: 13%">' +
            '           <span class="badge badge-light">${FILESIZE}</span>' +
            '         </td>' +
            '         <td>' +
            '             <span class="badge badge-light">${SUBMISSIONTIME}</span>' +
            '         </td>' +
            '         <td class="project-actions text-right" style="width: 20%">' +
            '             <a value=${FILEPOSITION} onclick=toDownloadFile(this) class="btn btn-primary btn-sm" href="javascript:void(0)">' +
            '                 <i class="fas fa-download">' +
            '                  </i>' +
            '  下载' +
            '             </a>' +
            ' <a value=${FILEPOSITION} onclick=toDeleteFile(this) class="btn btn-danger btn-sm" href="javascript:void(0)">' +
            '     <i class="fas fa-trash">' +
            '     </i>' +
            '     删除' +
            '   </a>' +
            '</td>' +
            ' </tr>';
        fileNode = fileNode.replace(/\$\{TASKID\}/g, taskRecordJson.taskId);
        fileNode = fileNode.replace(/\$\{TASKSTARTDATE\}/g, new Date(taskRecordJson.task.taskStartDate).format('Y-m-d H:i:s'));
        fileNode = fileNode.replace(/\$\{TASKTITLE\}/g, taskRecordJson.task.taskTitle);
        fileNode = fileNode.replace(/\$\{USERNUMBER\}/g, taskRecordJson.student.userNumber);
        fileNode = fileNode.replace(/\$\{USERNAME\}/g, taskRecordJson.student.userName);
        fileNode = fileNode.replace(/\$\{FILESIZE\}/g, taskRecordJson.fileSize / 1000 + 'KB');
        fileNode = fileNode.replace(/\$\{SUBMISSIONTIME\}/g, new Date(taskRecordJson.submissionTime).format('Y-m-d H:i:s'));
        fileNode = fileNode.replace(/\$\{FILEPOSITION\}/g, taskRecordJson.fileName);
        fileNode = fileNode.replace(/\$\{TASKRECORDID\}/g, taskRecordJson.recordId);

        return fileNode;
    }
    function toDeleteFile(obj) {
        _submitPost({ "fileName": obj.getAttribute("value"), "userId": getUserId(), "token": getUserToken() }, getUrl() + "/file/deleteFileUrlJson", function (results) {
            if (results.code == 1) {
                alert(results.msg);
                let fileName = obj.getAttribute('value');
                document.getElementById("taskRecord_" + fileName.substring(fileName.lastIndexOf("/") + 1)).remove();
            } else {
                alert("删除失败")
            }
        });
    }

    function toDownloadFile(obj) {
        _requestPost({ "fileName": obj.getAttribute("value") }, getUrl() + "/file/getSignedUrlJson", function (params) {
            download(params.substring(1, params.length - 1), obj.getAttribute("value").substring(obj.getAttribute("value").lastIndexOf("/") + 1));
        });
    }


</script>